package com.iot.inter.controller.newinterface.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.order.orderpackages.request.RequestOrderPackageNew;
import com.iot.inter.entity.newrequest.order.orderpackages.response.ResponseOrderPackageNew;
import com.iot.inter.interfaceprocessing.newinterface.TurnToOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.10 新订购套餐：2.0接口
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "3.10 新订购套餐")
public class OrderPackageNewController {

    private static Logger logger = Logger.getLogger(OrderPackageNewController.class);

    @CrossOrigin
    @RequestMapping(value = "orderPackageNew", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.10 新订购套餐", response = ResponseOrderPackageNew.class)
    public String order(@ApiParam @RequestBody RequestOrderPackageNew requestOrderPackage) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(requestOrderPackage);
        if (requestStr != null) {

            logger.info("【新订购套餐】请求：" + requestStr);

            RequestOrderPackageNew requestPackage = JSONArray.parseObject(requestStr, RequestOrderPackageNew.class);
            String deviceId = requestPackage.getDeviceId();
            String globalOrder = requestPackage.getGlobalOrder();
            String packageName = requestPackage.getPackageName();
            String packageCode = requestPackage.getPackageCode();
            String packageType = requestPackage.getPackageType();
            String orderPeriod = requestPackage.getOrderPeriod();
            String amount = requestPackage.getAmount();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            //现在去北京拿单号
            TurnToOrderService turnToOrderService = new TurnToOrderService();
            OrderPaymentManager manager = turnToOrderService.orderPackage(deviceId, globalOrder, packageName, packageCode, packageType, orderPeriod, amount, secret, partnerCode);

            if (manager == null) {
                ResponseOrderPackageNew responseOrder = new ResponseOrderPackageNew();
                responseOrder.setRetCode("9999");
                responseOrder.setResultInfo("接口返回有误");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【新订购套餐】响应：" + jsonString);
                return jsonString;


            } else if (true == Constant.RET_CODE_0000.equalsIgnoreCase(manager.getRetCode())) {

                ResponseOrderPackageNew responseOrder = new ResponseOrderPackageNew();
                responseOrder.setRetCode(manager.getRetCode());
                responseOrder.setResultInfo(manager.getResultInfo());
                responseOrder.setOrderId(manager.getOrderId());
                responseOrder.setOrderStartDate(manager.getOrderStartDate());
                responseOrder.setOrderExpireDate(manager.getOrderExpireDate());
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【新订购套餐】成功响应：" + jsonString);
                return jsonString;

            } else {
                ResponseOrderPackageNew responseOrder = new ResponseOrderPackageNew();
                responseOrder.setRetCode(manager.getRetCode());
                responseOrder.setResultInfo(manager.getResultInfo());
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【新订购套餐】响应：" + jsonString);
                return jsonString;

            }

        } else {
            ResponseOrderPackageNew responseOrder = new ResponseOrderPackageNew();
            responseOrder.setRetCode("9998");
            responseOrder.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【新订购套餐】响应：" + jsonString);
            return jsonString;
        }

    }


}
